/* **********************************************************
 * Copyright 2010 Dimitar Vangelovski
 *
 * This file is part of the Gens3 3D Engine.
 * 
 * The Gens3 3D Engine is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 * 
 * The Gens3 3D Engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with the Gens3 3D Engine; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 * **********************************************************/


#include "common.h"
#include "AnimObject.h"
#include "3DMath.h"

//extern float	g_FrameInterval;

CAnimObject::CAnimObject(void)
{
	m_VertexList.clear();
	m_iObjectType = -1;
	m_iSceneObjIndex = -1;

	m_RotCenter = CVertex(0.0f, 0.0f, 0.0f);
	ax = 0.0f;
	ay = 0.0f;
	az = 0.0f;
	
	tx = 0.0f;
	ty = 0.0f;
	tz = 0.0f;
	m_fTrSpeed = 0.0f;
	m_fTrRange = 0.0f;

	cax = 0.0f;
	cay = 0.0f;
	caz = 0.0f;
	
	ctx = 0.0f;
	cty = 0.0f;
	ctz = 0.0f;

	m_fSta = 0.0f;
}

CAnimObject::CAnimObject(const CAnimObject &other)
{
	m_VertexList = other.GetVertexList();
	m_iObjectType = other.GetObjectType();
	m_iSceneObjIndex = other.GetSceneObjIndex();

	m_RotCenter = other.m_RotCenter;
	ax = other.ax;
	ay = other.ay;
	az = other.az;
	
	tx = other.tx;
	ty = other.ty;
	tz = other.tz;
	m_fTrSpeed = other.m_fTrSpeed;
	m_fTrRange = other.m_fTrRange;

	cax = 0.0f;
	cay = 0.0f;
	caz = 0.0f;
	
	ctx = 0.0f;
	cty = 0.0f;
	ctz = 0.0f;

	m_fSta = 0.0f;
}

CAnimObject::~CAnimObject(void)
{
	m_VertexList.clear();
}

std::vector<CVertex> CAnimObject::Animate(float fSpeed)
{
	CVector vTemp;
	std::vector<CVertex> tmpVertList;

	unsigned int iSize = m_VertexList.size();

	tmpVertList.clear();
	tmpVertList.resize(iSize);

	//////////////////////////////////////////////////////////////////////////////////////////////
	// Rotate the object around a center point

	cax += Deg2Rad(ax) * fSpeed /** g_FrameInterval*/;
	cay += Deg2Rad(ay) * fSpeed /** g_FrameInterval*/;
	caz += Deg2Rad(az) * fSpeed /** g_FrameInterval*/;

	for (unsigned int i = 0; i < iSize; i++)
	{
		//vTemp = RotatePointAroundPoint(CVector(m_VertexList[i].x, m_VertexList[i].y, m_VertexList[i].z), CVector(m_RotCenter.x, m_RotCenter.y, m_RotCenter.z), CVector(cax, cay, caz));
		vTemp = RotatePointAroundPoint_Matrix(CVector(m_VertexList[i].x, m_VertexList[i].y, m_VertexList[i].z), CVector(m_RotCenter.x, m_RotCenter.y, m_RotCenter.z), CVector(cax, cay, caz));
		tmpVertList[i] = CVertex(vTemp.x, vTemp.y, vTemp.z);
	}

	//////////////////////////////////////////////////////////////////////////////////////////////
	// Translate the object using a sinus animation to get a bounce effect
	
	float fSinTrans = m_fTrRange * sin(Deg2Rad(m_fSta));

	ctx = tx * fSinTrans;
	cty = ty * fSinTrans;
	ctz = tz * fSinTrans;
	m_fSta += m_fTrSpeed * fSpeed /** g_FrameInterval*/;

	for (unsigned int i = 0; i < iSize; i++)
	{
		tmpVertList[i].x += ctx;
		tmpVertList[i].y += cty;
		tmpVertList[i].z += ctz;
	}

	return tmpVertList;
}
